﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TfsGetBuildDrop.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Init the command line options
            var options = new Options();

            //Parse the command line arguments
            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                //Command line sintax are valid and start main command
                ExecuteDownloadCommand(options);
            }
            else
            {

                //Command line sintax are not valid
                Console.WriteLine(options.GetUsage());
#if DEBUG
                Console.ReadKey();
#endif
            }
        }

        static void ExecuteDownloadCommand(Options options)
        {
            //Init the manager class for the specified TFS build
            TfsGetBuildDrop.Core.TfsProjectBuildManager t = new Core.TfsProjectBuildManager();
            string buildNum = "";

            try
            {
                //Try connect to the TFS build
                t.Connect(options.Address, options.Project, options.BuildNumber, options.Username, options.Password);
            }
            catch (TfsGetBuildDrop.Core.Exceptions.TfsProjectBuildManagerConnectException ex)
            {
                Console.WriteLine(@"ERROR: Unable connect to the TFS server.");
                return;
            }

            try
            {
                buildNum = t.GetLastGoodBuildNumber();
            }
            catch (Exception ex)
            {

            }

            if (options.Browser)
            {
                try
                {
                    Process.Start(t.GetDropDownloadUrl(buildNum));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(@"ERROR: Unable to get the build donwload url.");
                    return;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(options.DecompresDir))
                {
                    string outputFile = "";

                    if (string.IsNullOrEmpty(options.FileName))
                    {
                        outputFile = Path.Combine(Environment.CurrentDirectory, buildNum + ".zip");
                    }
                    else
                    {
                        outputFile = options.FileName;
                    }

                    MemoryStream m = t.DownloadDrop(buildNum);
                    FileStream downloadFile = new FileStream(outputFile, FileMode.OpenOrCreate);
                    m.WriteTo(downloadFile);
                }
                else
                {
                    MemoryStream m = t.DownloadDrop(buildNum);
                    TfsGetBuildDrop.Core.CompressionUtlity.UnZipStream(m, options.DecompresDir);
                }
            }
        }
    }
}
